package com.sbc.user.controller;

import cn.hutool.core.util.StrUtil;
import com.sbc.convention.exception.ClientException;
import com.sbc.convention.result.Result;
import com.sbc.idempotent.annotation.Idempotent;
import com.sbc.idempotent.enums.IdempotentSceneEnum;
import com.sbc.idempotent.enums.IdempotentTypeEnum;
import com.sbc.log.annotation.ILog;
import com.sbc.user.pojo.vo.PassengerReqVo;
import com.sbc.user.pojo.vo.PassengerRespVo;
import com.sbc.user.service.PassengerService;
import com.sbc.web.Results;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 乘车人控制层
 * @author: songbaicheng
 * @create: 2024-05-02 23:03
 **/
@ILog
@RestController
@RequestMapping("/api/user/passenger")
@RequiredArgsConstructor
public class PassengerController {

    private final PassengerService passengerService;

    /**
     * 根据用户名查询乘车人列表
     */
    @GetMapping({"/query/", "/query/{username}"})
    public Result<List<PassengerRespVo>> listPassengerQueryByUsername(@PathVariable(value = "username", required = false) String username) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }
        return Results.success(passengerService.queryPassengerByUsernameList(username));
    }

    /**
     * 新增乘车人
     *
     * @param username     用户名
     * @param requestParam 乘车人信息
     * @return 添加成功或失败
     */
    @PostMapping({"/add/", "/add/{username}"})
    public Result<Void> savePassenger(@PathVariable(value = "username", required = false) String username,
                                      @Valid @RequestBody PassengerReqVo requestParam) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }
        passengerService.addPassenger(username, requestParam);
        return Results.success();
    }

    /**
     * 修改乘车人信息
     *
     * @param id           乘车人编号
     * @param requestParam 乘车人信息
     * @return 修改成功或失败
     */
    @Idempotent(
            uniqueKeyPrefix = "12306-user:lock_passenger-alter:",
            key = "${username}",
            type = IdempotentTypeEnum.SPEL,
            scene = IdempotentSceneEnum.RESTAPI,
            message = "正在修改乘车人，请稍后再试..."
    )
    @PutMapping({"/update//",
            "/update/{username}/",
            "/update/{username}/{id}"})
    public Result<Void> updatePassenger(@PathVariable(value = "id", required = false) String id,
                                        @PathVariable(value = "username", required = false) String username,
                                        @Valid @RequestBody PassengerReqVo requestParam) {

        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }
        if (StrUtil.isEmpty(id)) {
            throw new ClientException("未知乘车人！");
        }
        passengerService.updatePassenger(username, id, requestParam);
        return Results.success();
    }

    /**
     * 删除乘车人
     *
     * @param id 乘车人编号
     * @return 删除成功或失败
     */
    @DeleteMapping({"/delete//",
            "/delete/{username}/",
            "/delete/{username}/{id}"})
    public Result<Void> deletePassenger(@PathVariable(value = "id", required = false) String id,
                                        @PathVariable(value = "username", required = false) String username) {
        if (StrUtil.isEmpty(username)) {
            throw new ClientException("用户名为空！");
        }
        if (StrUtil.isEmpty(id)) {
            throw new ClientException("未知乘车人！");
        }
        passengerService.deletePassenger(username, id);
        return Results.success();
    }
}
